/**
 * 
 */
package support.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import support.dao.BookDao;
import support.domain.Book;

/**
 * HibernateTemplate
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-2-9
 */

public class BookDaoImpl extends HibernateDaoSupport implements BookDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see support.dao.BookDao#save(support.domain.Book)
	 */
	public void save(Book book) {
		getHibernateTemplate().save(book);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see support.dao.jdbc.BookDao#delete(support.domain.Book)
	 */
	public void delete(Book book) {
		getHibernateTemplate().delete(book);
	}

	@SuppressWarnings("unchecked")
	public List<Book> findAllBook() {
		final String sql = "from Book";
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery(sql).setFirstResult(1)
						.setMaxResults(6).list();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see support.dao.jdbc.BookDao#findBookById(java.lang.Integer)
	 */
	public Book findBookById(Integer id) {
		return (Book) getHibernateTemplate().get(Book.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see support.dao.jdbc.BookDao#findBookByPrice(double, double)
	 */
	@SuppressWarnings("unchecked")
	public List<Book> findBookByPrice(double from, double too) {
		String sql = "from Book b where b.price between ? and ?";
		return getHibernateTemplate().find(sql, new Object[] { from, too });
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see support.dao.jdbc.BookDao#update(support.domain.Book)
	 */
	public void update(Book book) {
		getHibernateTemplate().update(book);
	}

}
